package com.linkoog.devtools.maven.dependency_folding;

import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import lombok.Getter;

import javax.swing.*;

import static com.linkoog.devtools.maven.dependency_folding.SettingsState.DEFAULT_COLLAPSED_BY_DEFAULT;
import static com.linkoog.devtools.maven.dependency_folding.SettingsState.DEFAULT_COLLAPSED_TEXT;

public class SettingsComponent {
    private final JBTextField collapsedTextField;
    private final JBCheckBox collapsedByDefaultField;
    @Getter
    private final JPanel panel;

    public SettingsComponent() {
        collapsedTextField = new JBTextField();
        collapsedByDefaultField = new JBCheckBox("Collapse dependencies by default");

        JButton restoreButton = new JButton("Restore default settings");
        restoreButton.addActionListener(e -> {
            collapsedTextField.setText(DEFAULT_COLLAPSED_TEXT);
            collapsedByDefaultField.setSelected(DEFAULT_COLLAPSED_BY_DEFAULT);
        });

        panel = FormBuilder.createFormBuilder()
                .addComponent(collapsedByDefaultField, 1)
                .addSeparator()
                .addLabeledComponent("Collapsed text format: ", collapsedTextField, 1, true)
                .addComponent(
                        new JBLabel(
                                "<html>\n" +
                                        "The collapsed text supports the following replacements:<br>\n" +
                                        "${node|replacement|notFound}<br>\n" +
                                        "${node|replacement}<br>\n" +
                                        "${node}<br>\n" +
                                        "<br>\n" +
                                        "Where:<br>\n" +
                                        "- node is the name of a node to find from the dependency itself.<br>\n" +
                                        "Example: \"artifactId\"<br>\n" +
                                        "- replacement is a formatting string that will be used when the node exists, with the node text as parameter.<br>\n" +
                                        "Example: \"[%s]\"<br>\n" +
                                        "If replacement is not present, it is considered as \"%s\"<br>\n" +
                                        "- notFound is a string that will be used if node is not found.<br>\n" +
                                        "Example: \"not found\"<br>\n" +
                                        "If notFound is not present it is considered as an empty string.<br>\n" +
                                        "You can use \\ to scape a character inside the brackets (like ${node|{%s\\}})<br>\n" +
                                        "Any other character outside will be shown without modification.<br>\n" +
                                        "<br>\n" +
                                        "Example:<br>\n" +
                                        "<br>\n" +
                                        "A pom.xml with<br>\n" +
                                        "<dependency><br>\n" +
                                        "<groupId>org.mapstruct</groupId><br>\n" +
                                        "<artifactId>mapstruct</artifactId><br>\n" +
                                        "<version>1.0</version><br>\n" +
                                        "</dependency><br>\n" +
                                        "<br>\n" +
                                        "with the replacement text:<br>\n" +
                                        "[${artifactId}] [${version|{%s\\}}] [${potato||potato not found}]<br>\n" +
                                        "<br>\n" +
                                        "Will result in the following collapsed text:<br>\n" +
                                        "[mapstruct] [{1.0}] [potato not found]<br>\n" +
                                        "</html>"
                        )
                )
                .addComponentFillVertically(new JPanel(), 0)
                .addComponent(restoreButton)
                .getPanel();
    }

    public JBTextField getPreferredFocusedComponent() {
        return collapsedTextField;
    }

    public String getCollapsedText() {
        return collapsedTextField.getText();
    }

    public void setCollapsedText(String newText) {
        collapsedTextField.setText(newText);
    }

    public boolean isCollapsedByDefault() {
        return collapsedByDefaultField.isSelected();
    }

    public void setCollapsedByDefault(boolean newStatus) {
        collapsedByDefaultField.setSelected(newStatus);
    }
}
